package fy.form.hr_plans.handler;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.collaboration.event.CollaborationCancelEvent;
import com.seeyon.apps.collaboration.event.CollaborationProcessEvent;
import com.seeyon.apps.collaboration.event.CollaborationStartEvent;
import com.seeyon.apps.collaboration.event.CollaborationStepBackEvent;
import com.seeyon.apps.collaboration.event.CollaborationStopEvent;
import com.seeyon.ctp.util.annotation.ListenEvent;

import fy.form.hr_plans.conf.Hr_PlansConf;
import fy.form.hr_plans.manager.PlansManager;

public class HRFormHandler {

	private static final Log logger = LogFactory.getLog(HRFormHandler.class);

	/**
	 * 发送表单事件
	 * 
	 * @param event
	 */
	@ListenEvent(event = CollaborationStartEvent.class, async = true)
	public void onFlowStart(CollaborationStartEvent event) {
		logger.info("A发送表单");
		PlansManager pm = new PlansManager();
		try {
			if (pm.isPersonalDevelopmentPlans(event.getSummaryId())) {
				pm.save(Hr_PlansConf.STATE_START, "发送表单");
			}
		} catch (Exception e) {
			logger.error("获取表单模板编号异常，异常表单SummaryID:" + event.getSummaryId(), e);
		}
	}

	/**
	 * 终止表单事件
	 * 
	 * @param event
	 */
	@ListenEvent(event = CollaborationStopEvent.class, async = true)
	public void onFlowStop(CollaborationStopEvent event) {
		logger.info("A终止表单");
		PlansManager pm = new PlansManager();
		try {
			if (pm.isPersonalDevelopmentPlans(event.getSummaryId(),
					event.getUserId())) {
				pm.save(Hr_PlansConf.STATE_STOP, "终止表单");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("获取表单模板编号异常，异常表单SummaryID:" + event.getSummaryId(), e);
		}
	}

	/**
	 * 撤销表单事件
	 * 
	 * @param event
	 */
	@ListenEvent(event = CollaborationCancelEvent.class, async = true)
	public void onFlowCancel(CollaborationCancelEvent event) {
		logger.info("A撤销表单");
		PlansManager pm = new PlansManager();
		try {
			if (pm.isPersonalDevelopmentPlans(event.getSummaryId(),
					event.getUserId())) {
				pm.save(Hr_PlansConf.STATE_CANCER, "撤销表单");
			}
		} catch (Exception e) {
			logger.error("获取表单模板编号异常，异常表单SummaryID:" + event.getSummaryId(), e);
		}
	}

	/**
	 * 表单回退事件
	 * 
	 * @param event
	 */
	@ListenEvent(event = CollaborationStepBackEvent.class, async = true)
	public void onFlowStepBack(CollaborationStepBackEvent event) {
		logger.info("A表单回退");
		PlansManager pm = new PlansManager();
		try {
			if (pm.isPersonalDevelopmentPlans(event.getSummaryId())) {
				pm.save(Hr_PlansConf.STATE_STEPBACK, "表单回退");
			}
		} catch (Exception e) {
			logger.error("获取表单模板编号异常，异常表单SummaryID:" + event.getSummaryId(), e);
		}
	}

	@ListenEvent(event = CollaborationProcessEvent.class, async = true)
	public void onPress(CollaborationProcessEvent event) {
		logger.info("A press");
		PlansManager pm = new PlansManager();
		if (pm.isPersonalDevelopmentPlans(event.getSummaryId())) {
			pm.save(Hr_PlansConf.STATE_PROCESS, "表单审批");
		}
	}

	@ListenEvent(event = com.seeyon.apps.collaboration.event.CollaborationFinishEvent.class, async = true)
	public void onFinish(
			com.seeyon.apps.collaboration.event.CollaborationFinishEvent event) {
		logger.info("A finish");
		PlansManager pm = new PlansManager();
		try {
			if (pm.isPersonalDevelopmentPlans(event.getSummaryId())) {
				pm.save(Hr_PlansConf.STATE_FINISH, "结束事件");
			}
		} catch (Exception e) {
			logger.error("判断事件状态发生异常，异常表单SummaryID:" + event.getSummaryId(), e);
		}
	}
}
